//
//  DayTwoViewController.m
//  LockTest
//
//  Created by ZhangBo on 2018/12/3.
//  Copyright © 2018 ZhangBo. All rights reserved.
//

#import "DayTwoViewController.h"
#import <Masonry/Masonry.h>
#import "NSObject+Sum.h"
#import "People.h"
#import "NSObject+WMKVO.h"
#import <ReactiveObjC/ReactiveObjC.h>

@interface DayTwoViewController ()
@property(nonatomic, copy) People *people;
@end

@implementation DayTwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self test_rac5];
}

- (void)test_rac1 {
    //创建一个View
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    //链式编程思想特点:方法返回值必须要有方法调用者!!

    //添加约束  --  make约束制造者!!
    [redView mas_makeConstraints:^(MASConstraintMaker *make) {
        //设置约束 每次调用left\top就是将约束添加到数组中!
        /*
         MASConstraint * (^block)(id)  = make.left.top.equalTo;
         MASConstraint * mk = block(@10);
         mk.top;
         */
        make.left.top.equalTo(@100);

        make.right.bottom.equalTo(@-100);
        /**
         mas_makeConstraints执行流程:
         1.创建约束制造者MASConstraintMaker,并且绑定控件,生成一个保存所有约束的数组
         2.执行mas_makeConstraints传入的Block
         3.让约束制造者安装约束!
         * 1.清空之前的所有约束
         * 2.遍历约束数组,一个一个安装
         */

    }];
}

- (void)test_rac2 {
    int result = [NSString hk_makeConstraints:^(SumManager *mgr) {
        mgr.add(10).add(20);
    }];
    NSLog(@"%d", result);
}

// 响应式编程
- (void)test_rac3 {
    People *p = [[People alloc] init];
    _people = p;
    //监听name属性
    [p addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
    //KVO底层实现
    //1.自定义Person类的子类!
    //2.重写setName:方法,在内部恢复父类做法,通知观察者
    //3.如何让外界调用自定义Person的子类方法,修改当前对象的isa指针,指向自定义的子类!
}

//一旦 p对象的name属性变化!!哥么就来!!set
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey, id> *)change context:(void *)context {
    NSLog(@"...%@", _people.name);
}

- (void)test_rac4 {
    People *p = [[People alloc] init];
    _people = p;
    //使用自定义的KVO来监听!Person 的 name 属性
    [p wm_addObserver:self forKeyPath:@"name" options:0 context:nil];
    NSLog(@"%@", [p class]);
}

//  函数式编程思想:是将操作尽可能写在一起!嵌套的函数!!
//  本质:就是往方法里面传入Block,方法中嵌套Block调用.

- (void)test_rac5 {
    SumManager *mgr = [[SumManager alloc] init];
    int result = [[mgr manager:^int(int result) {
        result += 10;
        result *= 2;
        return result;
    }] result];
    NSLog(@"%d", result);
}

- (void)test_rac6 {
    //1.创建信号
    RACReplaySubject *subject = [RACReplaySubject subject];

    //3.发送信号
    [subject sendNext:@1];

    //2.订阅信号
    [subject subscribeNext:^(id _Nullable x) {
        NSLog(@"%@", x);
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {
//    static NSInteger index = 0;
//    _people.name = [NSString stringWithFormat:@"%zi",index++];
//    [self test_rac6];
}

@end
